package cn.dakaqi.services.impl;

import cn.dakaqi.dao.AppVersionDao;
import cn.dakaqi.entities.AppVersion;
import cn.dakaqi.services.AppVersionService;
import cn.dakaqi.utils.exception.ServiceRuntimeException;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.List;
import java.util.Map;

/**
 * Copyright: Copyright (c)2016
 * Company: 志愿者打卡器(www.dakaqi.cn)
 * 类名称: AppVersionServiceImpl <br>
 * 类描述: <br>
 *
 * @author: ChunYang.Zhang
 * @since: 2016/8/3 15:17
 * @version: 1.0.0
 */
@Component(value = "appVersionService")
public class AppVersionServiceImpl implements AppVersionService
{
    @Autowired
    AppVersionDao appVersionDao;
    @Override
    public AppVersion findLastVersion(int code,String client) throws ServiceRuntimeException
    {
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        PageRequest pageRequest = new PageRequest(0, 20, sort);
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ,20));
        filters.put("code", new SearchFilter("code", SearchFilter.Operator.GT,code));
        filters.put("client", new SearchFilter("client", SearchFilter.Operator.EQ,client));

        Specification<AppVersion> spec = DynamicSpecifications.bySearchFilter(filters.values(), AppVersion.class);
        Page<AppVersion> data = appVersionDao.findAll(spec, pageRequest);
        List<AppVersion> list = data.getContent();
        if(null == list || list.size() == 0)
            return null;
        return list.get(0);
    }
}
